import router from "./router";
import store from "./store";
import { Message } from 'element-ui';
import { getToken } from '@/utils/token';
import {mapActions, mapGetters} from 'vuex';

/*进度条*/
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import de from "element-ui/src/locale/lang/de";

NProgress.configure({ showSpinner: false });

//白名单,放行的路由
const whiteList = ['/Login', '/Register'];
//浏览器默认title
const title = process.env.VUE_APP_TITLE;

router.beforeEach((to, from , next) => {
    //设置浏览器title
    // document.title = to.title ? to.title : title;
    document.title = title;

    NProgress.start(); //进度条打开
    //登录保存的token
    const token = getToken();
    if(token){
        //加载保存信息
        if (!store.state.user.hasUserAuth) {
            return store.dispatch('GET_USER_INFO', token).then(res => {
                console.log('用户信息获取成功')
                next({...to})
            }).catch(errMsg => {
                console.log('用户信息获取失败')
                Message.error(errMsg)
                // next('/403')
            })
        }
        if(to.path === '/Login'){
            next();
        }
        next();
    }else{
        if (whiteList.indexOf(to.path) !== -1) {
            next();
        } else {
            next({path: '/Login'});
            NProgress.done();
        }
    }

});

router.afterEach(() => {
    NProgress.done()
});

